#!/usr/bin/env python3
"""
插件加载序列文档查看器
"""

import sys
import os

def main():
    """主函数"""
    print("🔌 插件加载序列文档查看器")
    print("=" * 50)
    
    # 检查文档文件是否存在
    doc_file = "PLUGIN_LOADING_SEQUENCE.md"
    if not os.path.exists(doc_file):
        print(f"❌ 文档文件 {doc_file} 不存在")
        return
    
    print(f"📄 文档文件: {doc_file}")
    print(f"📊 文档大小: {os.path.getsize(doc_file)} 字节")
    
    # 显示文档的前几行
    print("\n📋 文档概览:")
    print("-" * 30)
    
    with open(doc_file, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        for i, line in enumerate(lines[:20], 1):
            print(f"{i:2d}: {line.rstrip()}")
    
    if len(lines) > 20:
        print(f"... (还有 {len(lines) - 20} 行)")
    
    print("\n🎯 文档章节:")
    print("-" * 30)
    
    # 提取标题
    with open(doc_file, 'r', encoding='utf-8') as f:
        content = f.read()
        import re
        headers = re.findall(r'^(#{1,6})\s+(.*?)$', content, re.MULTILINE)
        
        for level, title in headers:
            indent = "  " * (len(level) - 1)
            print(f"{indent}• {title}")
    
    print("\n🧪 运行验证测试:")
    print("-" * 30)
    
    # 添加当前目录到路径
    sys.path.insert(0, '.')
    
    try:
        print("开始导入 mypackage...")
        import mypackage
        
        print(f"✅ 插件管理器类型: {type(mypackage.plugin_manager)}")
        print(f"✅ 已加载插件: {mypackage.plugin_manager.list_plugins()}")
        
        # 验证插件实例
        plugin_a = mypackage.plugin_manager.get_plugin('plugin_a')
        plugin_b = mypackage.plugin_manager.get_plugin('plugin_b')
        
        if plugin_a and plugin_b:
            print(f"✅ Plugin A 名称: {plugin_a.plugin_instance.name}")
            print(f"✅ Plugin B 名称: {plugin_b.plugin_instance.name}")
            print("✅ 所有插件验证通过！")
        else:
            print("❌ 插件验证失败")
            
    except Exception as e:
        print(f"❌ 导入失败: {e}")
    
    print("\n📖 如何使用:")
    print("-" * 30)
    print("1. 阅读 PLUGIN_LOADING_SEQUENCE.md 了解详细的加载过程")
    print("2. 查看时序图理解类和方法的调用顺序")
    print("3. 运行验证代码确认加载序列")
    print("4. 使用调试技巧深入分析加载过程")
    
    print("\n🚀 相关文档:")
    print("-" * 30)
    related_docs = [
        "MODULE_LOADING_ORDER.md - 模块加载顺序总览",
        "PLUGIN_LOADING_SEQUENCE.md - 插件加载序列详解",
        "COMPARISON.md - 使用对比分析",
        "USAGE.md - 使用说明文档"
    ]
    
    for doc in related_docs:
        print(f"• {doc}")

if __name__ == "__main__":
    main() 